#include <iostream>

#include "AppClient.h"
#include "Slave.h"

using namespace std;

Slave slave;

json get_info() {
    json info;
    info["client_name"] = slave.getComputerName();
    info["client_master_volume"] = slave.getMasterVolume();
    info["client_uuid"] = "test-uuid";
    return info;
}

json get_master_volume() {
    json resp;
    resp["client_master_volume"] = slave.getMasterVolume();
    return resp;
}

void set_master_volume(int level) {
    slave.setMasterVolume(level);
}



int main(int argc, char* args) {
    AppClient client;
    client.onMessage([](const AppClient &c, json msg) {
        printf("receive msg from server %s\n", msg.dump().c_str());
        try {
            if (msg["code"] == CODE_COMMAND) {
                switch ((int) msg["command"]) {
                    case GET_INFO:
                        c.send(get_info());
                        break;
                    case SET_MASTER_VOLUME:
                        set_master_volume(atoi(msg["args"].get<vector<string>>()[0].c_str()));
                        break;
                    case GET_MASTER_VOLUME:
                        c.send(get_master_volume());
                        break;
                    default:
                        printf("unsupport command: %d", (int) msg["command"]);
                }
            } else {
                printf("unsupport request code: %d", (int) msg["code"]);
            }
        } catch (exception e) {
            printf("occurs exception: %s", e.what());
        }
    });
    client.connect(25539);
    while (1) hv_sleep(10);
    return 0;
}